from django.db import models
from dingxiang.utils.models import BaseModel
from stdimage import StdImageField
# 被mark_safe标记为安全的，Django将不会对其进行HTML转义，而是直接渲染为HTML代码。
from django.utils.safestring import mark_safe
from ckeditor.fields import RichTextField  # 不支持上传文件
from ckeditor_uploader.fields import RichTextUploadingField  # 支持上传文件


# Create your models here.

# 疾病方向表
class DiseaseDirectionModel(BaseModel):
    name = models.CharField(max_length=10, unique=True, verbose_name='方向名称')
    remark = models.TextField(max_length=256, null=True, blank=True, verbose_name='备注')

    class Meta:
        db_table = 'dx_disease_direction'
        verbose_name = '疾病方向表'
        verbose_name_plural = verbose_name


# 疾病分类表
class DiseaseCategoryModel(BaseModel):
    name = models.CharField(max_length=20, unique=True, verbose_name='分类名称')
    direction = models.ForeignKey('DiseaseDirectionModel', on_delete=models.DO_NOTHING, related_name='category_list',
                                  null=True, blank=True, verbose_name='所属方向')
    remark = models.TextField(max_length=256, null=True, blank=True, verbose_name='备注')

    class Meta:
        db_table = 'dx_disease_category'
        verbose_name = '疾病分类表'
        verbose_name_plural = verbose_name


# 疾病表
class DiseaseModel(BaseModel):
    name = models.CharField(max_length=20, unique=True, verbose_name='疾病名称')
    remark = models.TextField(max_length=256, null=True, blank=True, verbose_name='备注')
    direction = models.ForeignKey('DiseaseDirectionModel', db_constraint=False, on_delete=models.DO_NOTHING,
                                  related_name='disease_list', null=True, blank=True, verbose_name='所属方向')
    category = models.ForeignKey('DiseaseCategoryModel', db_constraint=False, on_delete=models.DO_NOTHING,
                                 related_name='disease_list', null=True, blank=True, verbose_name='所属分类')

    class Meta:
        db_table = 'dx_disease'
        verbose_name = '疾病表'
        verbose_name_plural = verbose_name


class DiseaseMessageModel(BaseModel):
    cover = StdImageField(upload_to='diseasecover/', null=True, blank=True, verbose_name='文章封面'
                          , variations={

                                    'thumb_600x400': {'width': 600, 'height': 400},
                                    'thumb_300x200': {'width': 300, 'height': 200},
                                    'thumb_100x100': {'width': 100, 'height': 100}

                            })
    content = RichTextUploadingField(default="", null=True, blank=True, verbose_name='内容')
    pub_date = models.DateField(auto_now=True, verbose_name='发布时间')
    author = models.CharField(max_length=20, null=True, blank=True, verbose_name='作者')

    # 方向，分类，疾病表外键
    direction = models.ForeignKey('DiseaseDirectionModel', db_constraint=False, on_delete=models.DO_NOTHING,
                                  related_name='message_list', null=True, blank=True, verbose_name='所属方向')
    category = models.ForeignKey('DiseaseCategoryModel', db_constraint=False, on_delete=models.DO_NOTHING,
                                 related_name='message_list', null=True, blank=True, verbose_name='所属分类')
    disease = models.ForeignKey('DiseaseModel', db_constraint=False, on_delete=models.DO_NOTHING,
                                related_name='message_list', null=True, blank=True, verbose_name='所属疾病')

    class Meta:
        db_table = 'dx_disease_message'
        verbose_name = '疾病文章表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 获取文章封面缩略图
    def message_cover_small(self):
        # 返回一个安全的HTML代码，其中包含一个img标签，src属性指向文章封面缩略图的URL，alt属性为"cover"
        return mark_safe(f'<img  alt="cover"  src="{self.cover.thumb_100x100.url}"></img>')

    # 图片的描述
    message_cover_small.short_description = "文章封面"
    # 图片是否显示
    message_cover_small.allow_tags = True
    # 设置排序顺序
    message_cover_small.order_field = 'cover'
